//
//  ZHTabBarController.m
//  XMLYFM
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "ZHTabBarController.h"
#import "UIButton+WebCache.h"
#import "ZHPlayViewController.h"

@interface ZHTabBarController ()

@property(nonatomic,retain)ZHCustomButton *previousBtn;
@property(nonatomic,retain)ZHTabBarPlayButton *playButton;

@end

@implementation ZHTabBarController


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([ZHPlayViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePlaying) {
        
        
        [self.playButton startCircle];
    }
    if ([ZHPlayViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePaused) {
        
        [self.playButton stopCircle];
        
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeImage:) name:@"changeImage" object:nil];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tabBarImageView = [[BaseImageView alloc] init];
    self.tabBarImageView.userInteractionEnabled = YES;
//    NSLog(@"zh= %@",self.tabBarImageView);

    [self isHiddenTabBar:NO withViewController:nil];
    
    // 下面的方法是调用自定义的生成按钮的方法
    [self creatButtonWithNormalName:@"tabbar_find_n@2x.png" andSelectName:@"tabbar_find_h@2x.png" andTitle:@"" andIndex:0];
    
    [self creatButtonWithNormalName:@"tabbar_sound_n@2x.png" andSelectName:@"tabbar_sound_h@2x.png" andTitle:@"" andIndex:1];
    
    [self creatButtonWithNormalName:@"tabbar_category_n@2x.png" andSelectName:@"tabbar_category_h@2x.png" andTitle:@"" andIndex:3];
    
    [self creatButtonWithNormalName:@"tabbar_me_n@2x.png" andSelectName:@"tabbar_me_h@2x.png" andTitle:@"" andIndex:4];
    
    //默认选中第一个按钮
    ZHCustomButton *btn = self.tabBarImageView.subviews[0];
    [self changeViewController:btn]; //自定义的控件中得按钮被点击了调用的方法，默认进入界面就选中第一个按钮
    
    
    self.playButton = [[ZHTabBarPlayButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_ZH/5, SCREEN_WIDTH_ZH/5)];
    [self.tabBarImageView addSubview:self.playButton];
    [self.playButton.musicButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
}
// 自定义设置tabBar是否隐藏
- (void)isHiddenTabBar:(BOOL)hidden withViewController:(UIViewController *)viewController{
    
    //先删掉自己自定义的
    [self.tabBarImageView removeFromSuperview];
    
    if (hidden) {
        
        [self.tabBar setHidden:NO];
        [self.tabBarImageView setFrame:self.tabBar.bounds];
        //关键****如果是隐藏就将自定义tabbar添加到系统的tabbar上
        [self.tabBar addSubview:self.tabBarImageView];
        if (viewController != nil) {
            viewController.hidesBottomBarWhenPushed = YES;
        }
        self.tabBar.backgroundColor = [UIColor clearColor];
        
    }else
    {
        [self.tabBar setHidden:YES];
        [self.tabBarImageView setFrame:self.tabBar.frame];
        
        //关键****如果是显示就将自定义tabbar添加到view上
        [self.view addSubview:self.tabBarImageView];
    }
   
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
  }
-(void)changeImage:(NSNotification *)notifition{
    [self.playButton.musicButton sd_setImageWithURL:notifition.object forState:UIControlStateNormal];
}



-(void)buttonAction:(UIButton *)btn{
    
    
//    TOPlayViewController *player = [TOPlayViewController sharaMusicPlayer];
//    TONavigationBarController *naVC = [[TONavigationBarController alloc] initWithRootViewController:player];
//    [self.view.window.rootViewController presentViewController:naVC animated:YES completion:^{
//        
//    }];
}

#pragma mark 创建按钮通用方法
- ( void )creatButtonWithNormalName:( NSString *)normal andSelectName:( NSString *)selected andTitle:( NSString *)title andIndex:( int )index

{

    ZHCustomButton *button = [ZHCustomButton buttonWithType:UIButtonTypeCustom ];
    
    button. tag = index;
    
    CGFloat buttonW = self.tabBarImageView.frame.size.width / 5;
    
    CGFloat buttonH = self.tabBarImageView.frame.size.height;
    button.frame = CGRectMake (SCREEN_WIDTH_ZH/5 * index, 0 , buttonW, buttonH);
    
    [button setImage :[[UIImage imageNamed:normal] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    [button setImage :[[UIImage imageNamed:selected] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState : UIControlStateDisabled];//设置按钮无效时的图片样式
    
    [button addTarget:self action:@selector (changeViewController:) forControlEvents : UIControlEventTouchDown];
 
    button.imageView.contentMode = UIViewContentModeCenter ; // 让图片在按钮内居中
    //    [button setTitle:title forState:UIControlStateNormal];
    //
    //    button.titleLabel.textAlignment = NSTextAlignmentCenter ; // 让标题在按钮内居中
    //
    //    button.titleLabel.font = [ UIFont systemFontOfSize:12]; // 设置标题的字体大小
    
    [self.tabBarImageView addSubview:button];
    
}

#pragma mark 按钮被点击时调用

- ( void )changeViewController:(ZHCustomButton *)sender

{
    
    //*****关键,设置选中的控制器
    self.selectedIndex = sender.tag ; //切换不同控制器的界面
//    NSLog(@"zh --- tabbar = %ld",sender.tag);
    sender.enabled = NO ;//让选中的按钮变为不可用,也就是限制点击一次
    
    //如果当前点击的按钮不是保存的按钮.那么将按钮设置为可用状态.
    //也就是两次点击的不是同一个按钮
    if ( self.previousBtn != sender) {
        
        self.previousBtn.enabled = YES ;
        
    }
    //记录当前点击的按钮
    self.previousBtn = sender;
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
